<template>
  <div>
    <BaseModal @onSubmit="onSubmit" @onClose="onClose" :show="show" title="编辑专业信息">
      <div class="demo-input-suffix">
        <span class="form-input-label">学校名称：</span>
        <el-autocomplete
          class="inline-input"
          v-model="selectedSchool"
          :fetch-suggestions="querySchool"
          placeholder="请输入学校名称"
          @select="selectSchool"
        ></el-autocomplete>
      </div>

      <div class="demo-input-suffix">
        <span class="form-input-label">学院名称：</span>
        <el-autocomplete
          class="inline-input"
          v-model="selectedCollege"
          :fetch-suggestions="queryCollege"
          placeholder="请输入学院名称"
          @select="selectCollege"
        ></el-autocomplete>
      </div>

      <div class="demo-input-suffix">
        <span class="form-input-label">专业名称：</span>
        <el-input v-model="data.major_name" placeholder="请输入专业名称"></el-input>
      </div>
    </BaseModal>
  </div>
</template>
<script>
import modalClose from "@/mixins/modalClose";
import CreateFilter from "@/mixins/CreateFilter";
import { majorModel } from "@/api/models";
import { Message } from "element-ui";
export default {
  mixins: [modalClose, CreateFilter],
  components: {},
  data() {
    return {
      data: {
        major_id: null,
        school_id: null,
        college_id: null,
        major_name: null
      }
    };
  },
  props: {
    obj: null
  },
  watch: {
    obj: function(value) {
      if(value.major_name) {
        if(value.major_name) {
          majorModel.item(value.major_id, res => {
            this.data = res.data
            this.selectedSchool = res.data.school_name
            this.selectedCollege = res.data.college_name
          })
        }
      }
    }
  },
  mounted() {},
  methods: {
    onSubmit() {
      let d = this.data;
      if (!d.major_name) {
        return Message.warning("请填完整信息");
      }
      if(d.major_id) {
        majorModel.update(d.major_id, d, res=> {
          Message.success(res.message);
          this.onClose();
          this.$emit('afterSubmit');
        })
      }else{
        majorModel.create(d, res=> {
          Message.success(res.message);
          this.$emit('afterSubmit');
        })
      }
    },
    onClose() {
      this.$emit("onClose");
    }
  }
};
</script>
<style lang="scss">
</style>